<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="renderer" content="webkit">
    <title>后台小类型页面</title>
    <meta name="keywords" content="">
    <meta name="description" content="">
    <link href="/after/css/bootstrap.min.css?v=3.4.0" rel="stylesheet">
    <link href="/after/css/font-awesome.min.css?v=4.3.0" rel="stylesheet">
    <link href="/after/css/plugins/dataTables/dataTables.bootstrap.css" rel="stylesheet">
    <link href="/after/css/animate.min.css" rel="stylesheet">
    <link href="/after/css/style.min.css?v=3.0.0" rel="stylesheet">

    <link href="/after/js/laypage/skin/laypage.css" rel="stylesheet">
    <!--引用vue的js-->
    <script src="/after/js/vue/vue.min.js"></script>
    <!--分页插件-->
    <script src="/after/js/laypage/laypage.js" charset="utf-8"></script>
    <script src="/after/js/layer/layer.js" charset="utf-8"></script>
</head>
<body class="gray-bg">
<div class="wrapper wrapper-content animated fadeInRight" id="app">
    <br>
    <br>
    <div class="row">
        <div class="col-sm-12">
            <div class="ibox float-e-margins">
                <h2>小类型操作
                    <div class="ibox-tools">
                        <a href="javascript:;" class="btn btn-primary btn-xs" onclick="tianjia();">添加新类型</a>
                    </div>
                </h2>
                <div class="ibox-title" id="bt">
                    <input type="button" value="搜索" onclick="getUserPageList();" />
                    <input type="text" id="type" name="type" style="text-align: left" placeholder="请输入要搜索的类型"/><br>
                    <input type="button" value="取消添加" onclick="quxiaotianjia();" style="display:none" id="qxtj" name="qxtj"/>
                    <div id="jiedian"></div>
                </div>
                <div class="ibox-content">
                    <table class="table table-striped table-bordered table-hover " id="editable">
                        <thead>
                        <tr>
                            <th>主键</th>
                            <th>小类型</th>
                            <th>外键</th>
                            <th colspan="2" style="text-align: center">操作</th>
                        </tr>
                        </thead>
                        <tbody>
                        <tr class="active" v-for="(item,index) in result">
                            <td>{{item.id}}</td>
                            <td>{{item.type}}</td>
                            <td>{{item.btype_id}}</td>
                            <td>
                                <a href="#" @click="editEvent(item)">编辑</a>
                            </td>
                            <td>
                                <a href="#" @click="delEvent(item.id)">删除</a>
                            </td>
                        </tr>
                        </tbody>

                    </table>
                <div id="pagenav"></div>
                </div>
            </div>
        </div>
    </div>
</div>
<!-- 全局js-->
<script src="/after/js/jquery-2.1.1.min.js"></script>
<script src="/after/js/bootstrap.min.js?v=3.4.0"></script>
<script src="/after/js/plugins/jeditable/jquery.jeditable.js"></script>
<!--返回主界面js
<script src="js/content.min.js?v=1.0.0"></script>-->
<!--查询分页js-->
<script>
    var app = new Vue({
        el : '#app',
        data : {
            result : []
        }
    });
    var getUserPageList = function(curr) {
        $.ajax({
            url:"/cf/queryPage.do",
            type:"post",
            dataType:"JSON",
            data : {
                pageNum:curr || 1,
                pageSize:4,
                type:$("#type").val()
            },
            error:function () {
                alert("错误");
            },
            success:function (msg) {
                app.result=msg.page;
                laypage({
                    cont : 'pagenav', //容器。值支持id名、原生dom对象，jquery对象,
                    pages : msg.totalPage, //总页数
                    skin:'#FFFFFF',
                    first : '首页',
                    last : '尾页',
                    curr : curr || 1, //当前页
                    jump : function(obj, first) { //触发分页后的回调
                        if (!first) { //点击跳页触发函数自身，并传递当前页：obj.curr
                            getUserPageList(obj.curr);
                        }
                    }
                });
            }
        });
    }
    getUserPageList();
    <!--添加-->
    var kaiguan=1;
    function tianjia(){
        document.getElementById("qxtj").style.display="block";
        if (kaiguan==1){
            var _tr = "<tr>" +
                    "<td>"+
                    "<input value='' type='text' onchange='' id='tpe' placeholder='请输入要添加的小类型'/>"+
                    "<input value='' type='text' onchange='jia();' id='waijian' placeholder='小类型对应的大类型'/>"+
                    "</td>"+
                    "</tr>";
            $("#bt").append(_tr);
            kaiguan=0;
        }else {
            alert("只允许一次添加!");
        }
    }
    function jia(){
        var stype=$("#tpe").val();
        var sid=$("#waijian").val();
        if(stype=="" ||sid==""){
            alert("不能为空");
        }else {
            $.ajax({
                url:'/cf/jia.do',
                type:'post',
                dataType:'json',
                data:{
                    type:stype,
                    btype_id:sid
                },
                error:function () {
                    alert("错误");
                },
                success:function (msg) {
                    $("#tpe").remove();
                    $("#waijian").remove();
                    getUserPageList();
                    kaiguan=1;
                    document.getElementById("qxtj").style.display="none";
                }
            });
        }
    }
    <!--取消添加-->
    function quxiaotianjia(){
        $("#tpe").remove();
        $("#waijian").remove();
        getUserPageList();
        kaiguan=1;
        document.getElementById("qxtj").style.display="none";
    }
    <!--删除-->
    function delEvent(obj){
        $.ajax({
            url:"/cf/shan.do",
            type:'post',
            dataType:'json',
            data:{
                id:obj
            },
            error:function () {
                alert("错误");
            },
            success:function (msg) {
                getUserPageList();
            }
        })
    }
    <!--修改-->
    function editEvent(obj) {
        var ht = "";
        var _tr = "<tr>" +
                "<td>"+
                "<input value='"+obj.id+"' type='text' disabled='disabled' id='id'/>"+
                "</td>"+
                "<td>"+
                "<input value='"+obj.type+"' type='text' onchange='xiugai();' id='tp'/>"+
                "</td>"+
                "</tr>";
        //拼接节点
        ht = ht+_tr;
        $("#jiedian").html(ht);
    }
    function xiugai(){
        var id=$("#id").val();
        var type=$("#tp").val();
        $.ajax({
            url:'/cf/upda.do',
            type:'post',
            dataType:'json',
            data:{
                id:id,
                type:type
            },
            error:function () {
                alert("错误");
            },
            success:function (msg) {
                $("#jiedian").empty();
                getUserPageList();
            }
        });
    }
</script>
</body>
</html>